package test;


import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import test.annotations.BeforeRequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * Created by Administrator on 2016/9/27.
 */

/**
 * 在拦截器中加上 bean
 */
public class ValidateInterceptor extends HandlerInterceptorAdapter {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        // TODO: 2016/10/6 请求域拦截
        BeforeRequestMapping annotation = method.getAnnotation(BeforeRequestMapping.class);
        if(annotation != null){
            // TODO: 2016/10/6 获取注解
            Class<? extends BaseValidate> value = annotation.value();
            BaseValidate baseValidator = value.newInstance();
            baseValidator.setRequest(request);
            baseValidator.setResponse(response);
            baseValidator.run();
        }

        return true;
    }
}
